package fileIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ObjectIO {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// test();
		// test1();
		// test2();
//		test3();
		test4();
	}

	private static void test4() throws FileNotFoundException, IOException, ClassNotFoundException {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("张狂", "男", 20));
		list.add(new Student("李明", "女", 22));
		list.add(new Student("王可金", "男", 29));
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				"/home/tarena/Desktop/18.txt",true));//使用追加功能，再追加时会内容时会增加序列化头，追加的内容要new ObjectInputStream(fin)
		out.writeObject(list);
		out.close();
		FileInputStream fin =new FileInputStream(
		"/home/tarena/Desktop/18.txt");
		ObjectInputStream in = new ObjectInputStream(fin);
		System.out.println(in.readObject());
//		in = new ObjectInputStream(fin);//
//		System.out.println(in.readObject());
		
		
		in.close();
	}

	private static void test3() throws FileNotFoundException, IOException, ClassNotFoundException {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("张狂", "男", 20));
		list.add(new Student("李明", "女", 22));
		list.add(new Student("王可金", "男", 29));
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				"/home/tarena/Desktop/18.txt",true));//不能使用追加功能，再次追加内容会多一个序列化头
		out.writeObject(list);//读取时只要读一次，将整个集合读出
		out.close();
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				"/home/tarena/Desktop/18.txt"));
		System.out.println(in.readObject());
		System.out.println(in.readObject());
//		System.out.println(in.readObject());
//		System.out.println(in.readObject());
		in.close();
	}

	private static void test2() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		Student stu = new Student("mark", "nan", 30);
		Student stu1 = new Student("jerry", "nv", 31);
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				"/home/tarena/Desktop/8.txt"));
		out.writeObject(stu);
		out.writeObject(stu1);
		// out.reset();
		File f = new File("/home/tarena/Desktop/8.txt");
		out.writeObject(f);
		Date d = new Date();
		out.writeObject(d);
		out.close();
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				"/home/tarena/Desktop/8.txt"));
		System.out.println(in.readObject());
		System.out.println(in.readObject());
		System.out.println(in.readObject());
		System.out.println(in.readObject());
		in.close();
	}

	private static void test1() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				"/home/tarena/Desktop/8.txt"));
		Date d = (Date) in.readObject();
		in.close();
		System.out.println(d);
	}

	private static void test() throws IOException {
		Date d = new Date();
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				"/home/tarena/Desktop/8.txt"));
		out.writeObject(d);
		out.close();
	}

}
